package com.eye.catcher.controller;

import com.alibaba.fastjson.JSONObject;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.jwt.JwtProperties;
import com.eye.catcher.jwt.JwtTokenUtil;
import com.eye.catcher.page.BasePage;
import com.eye.catcher.page.PageInfo;
import com.eye.catcher.page.PageInfoDto;
import com.eye.catcher.param.CommonResponse;
import com.eye.catcher.utils.IpUtil;
import com.eye.catcher.common.RedisUtils;
import com.eye.catcher.rabbit.RabbitMessageUtils;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>
 *  基类
 * </p>
 *
 * @author huzhiwei
 * @since 2022-02-08
 */
public class BaseController {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected JwtTokenUtil jwtTokenUtil;
    @Autowired
    protected JwtProperties jwtProperties;
    @Autowired
    protected RedisUtils redisUtils;
    @Autowired
    private RabbitMessageUtils rabbitMessageUtils;

    private static final ExecutorService executorService = Executors.newScheduledThreadPool(10);

    protected CommonResponse commonResponse = null;

    public void sendInitRabbitMqLog(String methodCode, String moduleCode, String moduleName,
                                    String className, String userId, SbCode sbCode, String param, String remark){
        JSONObject userAgent = getUserAgent();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                rabbitMessageUtils.sendInitRabbitMqLog(methodCode,moduleCode,moduleName,className,userId, sbCode,param,remark,userAgent);
            }
        });
    }
    public void sendFinishRabbitMqLog(String methodCode,String moduleCode, String moduleName,
                                      String className, String userId, SbCode sbCode,String param,String remark,Object returnText){
        JSONObject userAgent = getUserAgent();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                rabbitMessageUtils.sendFinishRabbitMqLog(methodCode,moduleCode,moduleName,className,userId, sbCode,
                                    param,remark,returnText,userAgent);
            }
        });
    }
    public void sendAbnormalRabbitMqLog(String methodCode, String moduleCode, String moduleName,
                                        String className, SbCode sbCode, String userId, String errorInfo) {
        JSONObject userAgent = getUserAgent();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                rabbitMessageUtils.sendAbnormalRabbitMqLog(methodCode,moduleCode,moduleName,className,userId, sbCode,
                        errorInfo,userAgent);
            }
        });
    }
    public void sendLoginRabbitMqLog(String methodCode,String param,String moduleCode, String moduleName,
                         String className,Object returnText,String userId,String loginName){
        JSONObject userAgent = getUserAgent();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                rabbitMessageUtils.sendLoginRabbitMqLog(methodCode,moduleCode,moduleName,className,
                                param,returnText,userId,loginName,userAgent);
            }
        });
    }
    public String getLoginUserId(){
        String token = request.getHeader(jwtProperties.getHeader());
        token = token.substring(7);
        String userId = jwtTokenUtil.getUsernameFromToken(token);
        return userId;
    }
    public String getLoginUserId(String token){
        String userId = jwtTokenUtil.getUsernameFromToken(token);
        return userId;
    }
    public JSONObject getUserAgent(){
        JSONObject jsonObject = new JSONObject();
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("user-agent"));
        //客户端类型  手机、电脑、平板
        jsonObject.put("clientType",userAgent.getOperatingSystem().getDeviceType().toString());
        jsonObject.put("os",userAgent.getOperatingSystem().getName());//操作系统类型
        jsonObject.put("ip", IpUtil.getIpAddr(request));//请求ip
        jsonObject.put("browser",userAgent.getBrowser().toString());//浏览器类型
        return jsonObject;
    }

    protected BasePage getPageInfo(PageInfoDto pageInfoDto) {
        Integer pageNum = 1;
        Integer pageSize = PageInfo.DEFAULT_NAVIGATE_PAGES;
        if(null == pageInfoDto.getPageNum() || "".equals(pageInfoDto.getPageNum())
            || 0 == Integer.parseInt(pageInfoDto.getPageNum())
            || 0 > Integer.parseInt(pageInfoDto.getPageNum()) ){
            pageNum = 1;
        }else{
            pageNum = Integer.parseInt(pageInfoDto.getPageNum());
        }
        if(null == pageInfoDto.getPageSize() || "".equals(pageInfoDto.getPageSize())
                || 0 == Integer.parseInt(pageInfoDto.getPageSize())
                || 0 > Integer.parseInt(pageInfoDto.getPageSize()) ){
            pageSize = PageInfo.DEFAULT_NAVIGATE_PAGES;
        }else{
            pageSize = Integer.parseInt(pageInfoDto.getPageSize());
        }
        return BasePage.builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .build();
    }


}
